//
//  PersonalCenterViewController.m
//  WuShiCity
//
//  Created by chengjie on 16/7/4.
//  Copyright © 2016年 chengjie. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "SignStoreViewController.h"
#import "PublishCollectionViewCell.h"
#import "PersonalHeaderVIew.h"
#import "BaseCollectionViewCell.h"

@interface PersonalCenterViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIImageView *headImageView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UILabel *userName;

@end

@implementation PersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
        // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma Mark - -------UICollectionView的代理方法------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 6;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 3;
    } else if (section == 1){
        
        return 1;
    } else if (section == 2){
        
        return 7;
    }else {
        
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCollectionViewCell *baseCell = nil;
    PublishCollectionViewCell *CELL = [collectionView dequeueReusableCellWithReuseIdentifier:@"PublishCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        
        CELL.itemLabel.text = @"123";
    } else if (indexPath.section == 1){
        
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalSignCell" forIndexPath:indexPath];
        return baseCell;
    }else if (indexPath.section == 2) {
        
        baseCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
        baseCell.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
        return baseCell;
    }
        
    return CELL;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        
        SignStoreViewController *singStoreVC = [[SignStoreViewController alloc] init];
        [self.navigationController pushViewController:singStoreVC animated:NO];
    }
}

//注册头分区
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            
            [headerView addSubview:[self setPersonalHeaderView]];
        }if (indexPath.section == 1) {
            
            headerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
        }
        if (indexPath.section == 2) {
            
            headerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        }
        return headerView;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        return nil;
    }
    return nil;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    if (section == 2) {
        
        return 0;
    }else {
        
        return 10;
    }
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 2) {
        
        return 0;
    }else {
        
        return 10;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    if (section == 2) {
        
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }else {
        
        return UIEdgeInsetsMake(9, 9, 9, 9);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake((kScreenWidth-20)/4, (kScreenWidth-20)/4);
    }else if (indexPath.section == 1){
        
        return CGSizeMake(kScreenWidth, 80);
    } else if (indexPath.section == 2) {
        
        return CGSizeMake(kScreenWidth/4, kScreenWidth/4);
    }else {
        
        return CGSizeMake(0, 0);
    }
}

//头分区的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return CGSizeMake(kScreenWidth, 150);
    }else if (section == 1){
        
        return CGSizeMake(kScreenWidth, 1);
    }else {
        
        return CGSizeMake(kScreenWidth, 10);
    }
}
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    
//    
//}
#pragma mark - ------滚动视图的代理----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat yOffset = self.collectionView.contentOffset.y;

    //向上偏移量变正，向下偏移量变负
    if (yOffset < -20) {
        CGFloat factor = ABS(yOffset)+130-20;
        CGRect f = CGRectMake(-([[UIScreen mainScreen] bounds].size.width*factor/130-[[UIScreen mainScreen] bounds].size.width)/2,-ABS(yOffset)+20, [[UIScreen mainScreen] bounds].size.width*factor/130, factor);
        self.headImageView.frame = f;
    }else {
        CGRect f = self.headImageView.frame;
        f.origin.y = 0;
//        self.headImageView.frame = f;
        self.headImageView.frame = CGRectMake(0, f.origin.y, [[UIScreen mainScreen] bounds].size.width, 130);
    }
    
}

#pragma Mark - ----------设置UIView头视图---------
- (UIView *)setPersonalHeaderView {
    
    UINib *nib = [UINib nibWithNibName:@"PersonalHeaderVIew" bundle:nil];
    PersonalHeaderVIew *headerView = [[nib instantiateWithOwner:nil options:nil] firstObject];
    [headerView setFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    headerView.naVC = self.navigationController;
    self.headImageView = headerView.headerImageView;
    self.userName = headerView.personalName;
    return headerView;
}


#pragma mark - ---------laying----------
//collection第一分区头视图图片
- (UIImageView *)headImageView {
    
    if (!_headImageView) {
        
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}


- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-44) collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:@"PublishCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PublishCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalSignCollecionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalSignCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (UILabel *)userName {
    
    if (!_userName) {
        
        _userName = [[UILabel alloc] init];
    }
    return _userName;
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
